﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace TapiocaWpf.Views
{
    /// <summary>
    /// Interaction logic for AppSetting.xaml
    /// </summary>
    public partial class AppSetting : Window
    {
        public string Engine
        {
            get
            {
                return this.cbEngine.Text;
            }
        }

        public string FileType
        {
            get
            {
                return this.cbFileType.Text;
            }
        }

        public AppSetting()
        {
            InitializeComponent();

            //List<string> threshold = new List<string>();

            //threshold.Add("Off");
            //threshold.Add("Fatal");
            //threshold.Add("Error");
            //threshold.Add("Warn");
            //threshold.Add("Info");
            //threshold.Add("Debug");
            //threshold.Add("All");

            //cbThreshold.ItemsSource = threshold;

            //cbThreshold.DisplayMemberPath = "Name";
            //cbThreshold.SelectedValuePath = "Id";

            XDocument appConfigX = XDocument.Load("Log4Net.config");
            XElement level = appConfigX.Root.Element("log4net").Element("root").Element("level");

            for (int i = 0; i < cbThreshold.Items.Count; i++)
            {
                if ((cbThreshold.Items[i] as ComboBoxItem).Content.ToString().ToLower() == level.Attribute("value").Value.ToLower())
                {
                    cbThreshold.SelectedIndex = i;
                }
            }

            cbFileType.SelectedIndex = 0;
            cbEngine.SelectedIndex = 0;
        }


        private void SetLogLevel(string threshold)
        {
            XDocument appConfigX = XDocument.Load("Log4Net.config");
            XElement level = appConfigX.Root.Element("log4net").Element("root").Element("level");
            level.SetAttributeValue("value", threshold);
            appConfigX.Save("Log4Net.config");
        }


        private void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            SetLogLevel(cbThreshold.Text);

            this.Close();
        }


        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
